/*
 * @FilePath: \undefinedc:\Users\sxjct\.leetcode\82.删除排序链表中的重复元素-ii.cpp
 * @Brief: 
 * @Version: 1.0
 * @Date: 2021-03-03 14:10:35
 * @Author: tianyiyi
 * @Copyright: Copyright@tianyiyi
 * @LastEditors: Mr.Tian
 * @LastEditTime: 2021-03-25 10:48:40
 */
/*
 * @lc app=leetcode.cn id=82 lang=cpp
 *
 * [82] 删除排序链表中的重复元素 II
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
        ListNode* deleteDuplicates(ListNode* head) {
            ListNode* prehead = new ListNode(0);
            prehead->next = head;
            if(head == NULL)
                return NULL;
            ListNode *p = prehead;
            ListNode *r = head;
            ListNode *rr = r->next;;
            while(r!=NULL && r->next!=NULL)
            {
                if(rr->next!=NULL && r->val == rr->val)
                {
                    rr = rr->next;
                    continue;
                }else if(rr->next == NULL && r->val == rr->val)
                {
                    p->next = NULL;
                    r = NULL;
                    rr = NULL;
                }
                else if(rr->val != r->val && r->next != rr)
                {
                    p->next = rr;
                    r = rr;
                }else if(rr ->val != r->val && r->next == rr)
                {
                    p = r;
                    r = rr;
                    rr = r->next;
                }
            }
        return prehead->next;
    }
};
// @lc code=end

